<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Reference unpacker</title>
<script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>
<script>
function unpack(lookup, data) {
	var key, time, unpackTime, parseTime, uncompressed, lookupSize = 0, schema = {};

	// Calc lookup size
	for (key in lookup)
		lookupSize += key.length + lookup[key].length;

	function replace(value) {
		return value.replace(/[A-Z]+/g, function(key) {
			return replace(lookup[key]);
		});
	};

	function makeMap(str) {
		var items = str.split(/\|/), i, map = {};

		for (i = 0; i < items.length; i++)
			map[items[i]] = 1;

		return map;
	};

	time = new Date().getTime();

	// Unpack lookup
	for (key in lookup)
		lookup[key] = replace(lookup[key]);

	// Unpack data
	uncompressed = replace(data);
	unpackTime = new Date().getTime() - time;

	uncompressed.replace(/(\w+)\[([^\]]+)\]\[([^\]]+)\]/g, function(str, tag, attrs, children) {
		schema[tag] = {
			attribs : makeMap(attrs),
			children : makeMap(children)
		};
	});

	parseTime = new Date().getTime() - time;

	// Log results
	log('Unpacking took: ' + unpackTime + ' ms' + ', parsing took: ' +
		parseTime + ' ms, Total: ' + (unpackTime + parseTime) +
		' ms, ratio: ' +
		Math.round(100 - ((lookupSize + data.length) / uncompressed.length) * 100) + '%'
	);

	log('Data: ' + uncompressed + '\n');

	console.dir(schema);
}

function log(str) {
	document.getElementById('output').innerHTML += (str + "\n").replace(/\n/g, '<br />');
}
</script>
</head>
<body>
<pre id="output">
</pre>
<script src="dtds/xhtml1-strict.dtd.compressed.min.js"></script>
<script src="dtds/xhtml1-transitional.dtd.compressed.min.js"></script>
</body>
</html>
